document.addEventListener('DOMContentLoaded', function () {
    var menu = document.getElementById('menu');
    var drawer = document.getElementById('drawer');

    menu.addEventListener('click', function () {
        drawer.classList.toggle('open');
    });
});


const products = [
    {
        id: 1,
        name: '琴酒',
        description: '正品行货 添加利伦敦干味 金酒杜松子酒金汤力基酒 Gin琴酒进口洋酒',
        price: 88,
        imgSrc: 'image/琴酒.png'
    },
    {
        id: 2,
        name: '波本',
        description: '格兰杰波本雨燕马爹利波本VS雨燕700ML波本VS雨燕700ML 一瓶 L 无盒',
        price: 229,
        imgSrc: 'image/波本.png'
    },
    {
        id: 3,
        name: '伏特加',
        description: '苏连红伏特加 俄罗斯伏特加750ml 拉脱维亚伏特加酒吧鸡尾酒用酒',
        price: 44,
        imgSrc: 'image/伏特加.png'
    },
    {
        id: 4,
        name: '朗姆酒',
        description: '摩根船长（Captain Morgan）朗姆酒 mojito莫吉',
        price: 64,
        imgSrc: 'image/朗姆.png'
    },
    {
        id: 5,
        name: '黑麦威士忌',
        description: '杰克丹尼（Jack Daniels）黑麦 美国田纳西州 调和型威士忌 1000ml',
        price: 219,
        imgSrc: 'image/黑麦威士忌.png'
    },
    {
        id: 6,
        name: '苏格兰威士忌',
        description: 'THE MACALLAN麦卡伦12年 经典雪莉桶 单一麦芽苏格兰威士忌',
        price: 781,
        imgSrc: 'image/苏格兰威士忌.png'
    },
    {
        id: 7,
        name: '白兰地',
        description: '人头马（Remy Martin）CLUB优质香槟区干邑白兰地洋酒350mL 1瓶',
        price: 153,
        imgSrc: 'image/白兰地.png'
    },
    {
        id: 8,
        name: '武陵酒',
        description: '武陵酒 经典飘香 纯粮食酱香白酒 湖南名酒 500ml*1瓶',
        price: 146,
        imgSrc: 'image/武陵酒.png'
    },
    {
        id: 9,
        name: '五粮液',
        description: '五粮液第八代普五 52度五粮液浓香型白酒 500ML*1瓶 送礼宴请浓香型白酒',
        price: 989,
        imgSrc: 'image/五粮液.png'
    },
    {
        id: 10,
        name: '龙舌兰',
        description: '洋酒 墨西哥进口 唐胡里奥珍藏金标龙舌兰 Don Julio 750ml',
        price: 438,
        imgSrc: 'image/龙舌兰.png'
    },
    {
        id: 11,
        name: '西凤酒',
        description: '西凤酒52度浓香型白酒整箱装粮食酒口粮酒婚宴喜酒白酒礼盒 500ml*1瓶',
        price: 146,
        imgSrc: 'image/西凤酒.png'
    },
    {
        id: 12,
        name: '古今贡酒',
        description: '古井贡酒 年份原浆献礼版 50度500ml*1瓶',
        price: 165,
        imgSrc: 'image/古今贡酒.png'
    },

];
let cart = [];
let totalPrice = 0;


function updateCartDisplay() {
    const cartItems = document.getElementById('cart-items');
    cartItems.innerHTML = '';

    cart.forEach((item, index) => {
        const li = document.createElement('li');
        li.style.color = 'white';
        li.style.fontSize = '1.5vw';
        li.innerHTML = `  
            <img src="${item.imgSrc}" alt="${item.name}" style="width:100px;">  
            ${item.name} - ${item.price}￥ <button class="remove-item" data-index="${index}">取消</button>  
        `;
        cartItems.appendChild(li);


        const removeButton = li.querySelector('.remove-item');
        removeButton.addEventListener('click', function () {
            const indexToRemove = this.getAttribute('data-index');
            cart.splice(parseInt(indexToRemove), 1);
            updateCartDisplay();
            updateTotalPrice();
        });
    });
}

function updateTotalPrice() {
    totalPrice = cart.reduce((acc, item) => acc + item.price, 0);
    document.getElementById('total-price').textContent = totalPrice.toFixed(2);
    document.getElementById('total-price1').textContent = totalPrice.toFixed(2);
}

totalPrice = 100;


document.querySelectorAll('.add-to-cart').forEach(function (button) {
    button.addEventListener('click', function () {

        const productId = button.closest('.product').getAttribute('data-product-id');


        const productToAdd = products.find(product => product.id === parseInt(productId));


        if (productToAdd) {
            cart.push(productToAdd);
            updateCartDisplay();
            updateTotalPrice();
        }
    });
});


updateCartDisplay();


document.getElementById('payment').onclick = function () {
    var img = document.getElementById('pay');
    img.style.display = img.style.display === 'none' ? 'block' : 'none';

}

var closeBtn = document.getElementById('closeBtn');


closeBtn.addEventListener('click', function () {

    var modal = document.getElementById('myModal');
    modal.style.display = 'none';

});


    window.onload = function() {
    var hash = window.location.hash;
    if (hash) {
    var element = document.getElementById(hash.substring(1));
    if (element) {
    element.scrollIntoView({behavior: 'smooth'});
}
}
};



